`
https://leetcode.cn/problems/flip-binary-tree-to-match-preorder-traversal/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number[]} voyage
 * @return {number[]}
 */
var flipMatchVoyage = function (root, voyage) {
  const res = []
  let i = 0
  let isMatch = true

  const traverse = (root) => {
    if (root === null) {
      return
    }

    // 如果结点的值与 voyage 的值对不上，说明无解
    if (root.val !== voyage[i++]) {
      isMatch = false
      return
    }

    // 如果左字节点的值与 voyage 的值对不上，翻转本结点
    if (root.left && root.left.val !== voyage[i]) {
      // 交换左右结点
      [root.left, root.right] = [root.right, root.left]
      // 记录翻转结点的值
      res.push(root.val)
    }

    // 递归遍历
    traverse(root.left)
    traverse(root.right)
  }

  traverse(root)

  if (isMatch) return res
  return [-1]
};